package com.spider.util;

import com.spider.service.CronMapperServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;


//@Configuration
public class ScheduleTask implements SchedulingConfigurer{

    @Autowired
    private  CronMapperServiceI csi;
    @Override
    public void configureTasks(ScheduledTaskRegistrar str){
        str.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                System.out.println("执行动态定时任务" +
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                String cronExpression = csi.getCron();
                if(StringUtils.isEmpty(cronExpression)){

                }
                return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);
            }
        });



        /*triggerContext ->{
            String cronExpression = csi.getCron();
            if(StringUtils.isEmpty(cronExpression)){

            }
            return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);
        }*/

    }

    //lambda 表达式
    /*public  void test(ScheduledTaskRegistrar str){
        str.addTriggerTask(
            () -> System.out.println(""),
            triggerContext ->{
                String cronExpression = csi.getCron();
                if(StringUtils.isEmpty(cronExpression)){

                }
                return new CronTrigger(cronExpression).nextExecutionTime(triggerContext);
            }
        );
    }*/
}
